package com.threeox.commonlibrary.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.entity.engine.ModelViewType;
import com.threeox.commonlibrary.ui.view.engineview.base.CommonModelView;
import com.threeox.commonlibrary.ui.view.engineview.listmodel.ListModelBaseView;
import com.threeox.commonlibrary.ui.view.engineview.model.ModelBaseView;
import com.threeox.commonlibrary.ui.view.engineview.multipagemodel.MultiPageModelView;
import com.threeox.commonlibrary.ui.view.engineview.tablemodel.TableModelView;

/**
 *
 * @ClassName: ModelDialog
 *
 * @Description: Todo(模型Dialog)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2018/10/25 7:01 PM
 * 
 * @version 1.0
 */
public class ModelDialog extends Dialog {

    private Context context;

    public static ModelDialog newInstance(Context context) {
        return new ModelDialog(context);
    }

    public static ModelDialog newInstance(Context context, int themeResId) {
        return new ModelDialog(context, themeResId);
    }

    private ModelDialog(Context context) {
        this(context, R.style.model_dialog_style);
    }

    private ModelDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initConfig();
    }

    private void initConfig() {
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, context.getResources().getDisplayMetrics());
        lp.width = (display.getWidth() - pageMargin); // 设置宽度
        getWindow().setAttributes(lp);
    }

    public ModelDialog initModelDialog(int modelId, ModelViewType viewType) {
        return initModelDialog(modelId, viewType, LinearLayout.LayoutParams.MATCH_PARENT);
    }

    /**
     * 初始化模型dialog
     *
     * @param modelId
     * @param viewType
     */
    public ModelDialog initModelDialog(int modelId, ModelViewType viewType, int height) {
        CommonModelView view = null;
        if (ModelViewType.ModelBaseView == viewType) {
            view = new ModelBaseView(context);
        } else if (ModelViewType.TableModelView == viewType) {
            view = new TableModelView(context);
        } else if (ModelViewType.ListModelBaseView == viewType) {
            view = new ListModelBaseView(context);
        } else if (ModelViewType.MultiPageModelView == viewType) {
            view = new MultiPageModelView(context);
        }
        Intent intent = new Intent();
        intent.putExtra(CommonModelView.FILENAMEMODE, modelId);
        view.initData(intent);
        return initDialogView(view, height);
    }

    /**
     * 初始化模型dialog
     *
     * @param view
     * @return
     */
    public ModelDialog initDialogView(View view) {
        return initDialogView(view, LinearLayout.LayoutParams.MATCH_PARENT);
    }


    /**
     * 初始化模型dialog
     *
     * @param view
     * @param height
     * @return
     */
    public ModelDialog initDialogView(View view, int height) {
        this.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height));// 设置布局
        return this;
    }

}
